package com.cmo.rzl.triage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.beetl.sql.core.kit.StringKit;

import com.cmo.rzl.core.base.BaseController;
import com.cmo.rzl.core.jfinal.ext.autoroute.ControllerBind;
import com.cmo.rzl.core.toolbox.Record;
import com.cmo.rzl.core.toolbox.kit.ShardKit;
import com.cmo.rzl.shine.model.Doctor2queuetype;
import com.cmo.rzl.shine.model.Triage;
import com.cmo.rzl.shine.service.Doctor2queuetypeService;
import com.cmo.rzl.shine.service.Pager2queuetypeService;
import com.cmo.rzl.shine.service.TriageService;
import com.cmo.rzl.shine.service.impl.Doctor2queuetypeServiceImpl;
import com.cmo.rzl.shine.service.impl.Pager2queuetypeServiceImpl;
import com.cmo.rzl.shine.service.impl.TriageServiceImpl;

@ControllerBind(controllerKey = "/paiban")
public class ClientPaibanController extends BaseController {

	ShardKit shared = new ShardKit();
	// 分诊台
	TriageService triage = new TriageServiceImpl();
	// 医生排班
	Doctor2queuetypeService rdq = new Doctor2queuetypeServiceImpl();
	// 叫号器排班
	Pager2queuetypeService rpq = new Pager2queuetypeServiceImpl();

	// 分诊台分诊类型按医生的排班 triage_type1按医生 2按叫号器
	public void DoctorOrPagerPaiban() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 按医生
		String ip = shared.getIpAddr(getRequest());
		if (StringKit.isBlank(ip)) {
			renderJson(error(NOT_NULL_MSG));
			return;
		}
		System.out.println("ip:" + ip);
		Triage rip = triage.queryTriageIp(ip);
		if (null == rip) {
			map.put("return_code", "fail");
			map.put("return_msg", "当前电脑没有分诊台配置");
			renderJson(map);
			return;
		} else {
			String tip = rip.getIp();
			if (StringKit.isBlank(tip)) {
				renderJson(error(NOT_NULL_MSG));
				return;
			}
			if ("1".equals(rip.getTriage_type().toString())) {
				List<Record> list = rdq.findDoctorPaiban(tip);
				if (null == list) {
					map.put("return_code", "fail");
					map.put("return_msg", "查询按医生类型排班失败或没有排班信息");
					renderJson(map);
					return;
				} else {
					map.put("return_code", "success");
					map.put("return_msg", "查询按医生类型排班成功");
					map.put("list", list);
					map.put("count", list.size());
					renderJson(map);
				}
			} else if ("2".equals(rip.getTriage_type().toString())) {// 按叫号器
				List<Record> list = rpq.pager_paiban(tip);
				if (null == list) {
					map.put("return_code", "fail");
					map.put("return_msg", "查询按叫号器类型排班失败或没有排班信息");
					renderJson(map);
					return;
				} else {
					map.put("return_code", "success");
					map.put("return_msg", "查询按叫号器排班信息成功");
					map.put("list", list);
					map.put("count", list.size());
					renderJson(map);
				}
			} else {
				map.put("return_code", "fail");
				map.put("return_msg", "系统错误，请联系管理员");
				renderJson(map);
				return;
			}
		}
	}
	//修改排班
	public void update_pb(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ids=getPara("ids");
		if(StringKit.isBlank(ids)){
			map.put("return_code", "fail");
			map.put("return_msg", "ID不能为空");
			renderJson(map);
			return;
		}
		ids = ids.substring(0,ids.length()-1);
		String removestr="d_";
		ids=ids.replaceAll(removestr, "");
		String onduty=getPara("onduty");
		if(StringKit.isBlank(onduty)){
			map.put("return_code", "fail");
			map.put("return_msg", "排班不能为空");
			renderJson(map);
			return;
		}
		boolean bool= rdq.update_rdq(ids, onduty);
		if(bool){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
	}
	//删除排班
	public void delete_pb(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ids=getPara("ids");
		if(StringKit.isBlank(ids)){
			map.put("return_code", "fail");
			map.put("return_msg", "不能为空");
			renderJson(map);
			return;
		}
		ids = ids.substring(0,ids.length()-1);
		String removestr="d_";
		ids=ids.replace(removestr, "");
		int rdqnum= rdq.delete_pb(ids);
		if(rdqnum>0){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
	}
	//手动添加排班
	public void DoctorOrPagerPaibanAdd(){
		Map<String, Object> map=new HashMap<String, Object>();
		String doctor_id=getPara("doctor_id");
		if(StringKit.isBlank(doctor_id)){
			map.put("return_code", "fail");
			map.put("return_msg", "医生不能为空");
			renderJson(map);
			return;
		}
		String queue_type_id=getPara("queue_type_id");
		if(StringKit.isBlank(queue_type_id)){
			map.put("return_code", "fail");
			map.put("return_msg", "队列不能为空");
			renderJson(map);
			return;
		}
		String onduty=getPara("onduty");
		if(StringKit.isBlank(onduty)){
			map.put("return_code", "fail");
			map.put("return_msg", "排班不能为空");
			renderJson(map);
			return;
		}
		Record record= rdq.finddoctorandqueuetypeid(doctor_id, queue_type_id);
		if(null!=record){
			map.put("return_code", "fail");
			map.put("return_msg", "当前医生排班已存在");
			renderJson(map);
			return;
		}
		Doctor2queuetype d=new Doctor2queuetype();
		d.setDoctor_id(Integer.parseInt(doctor_id));
		d.setQueue_type_id(Integer.parseInt(queue_type_id));
		d.setOnduty(onduty);
		boolean bool= rdq.save(d);
		if(bool){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
	}
	//查询当前分诊台下的队列
	public void list_paiban(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ip=shared.getIpAddr(getRequest());
		if(StringKit.isBlank(ip)){
			map.put("return_code", "fail");
			map.put("return_msg", "获取分诊台ip失败");
			renderJson(map);
			return;
		}
		List<Record> list= triage.queryByQueueType(ip);
		if(null==list||list.size()<1){
			map.put("return_code", "fail");
			map.put("return_msg", "当前分诊台下没有队列列表");
			renderJson(map);
			return;
		}else{
			map.put("return_code", "success");
			map.put("return_msg", "查询成功");
			map.put("list",list);
			renderJson(map);
		}
	}
}
